#define _CRT_SECURE_NO_WARNINGS 1		
#include"SLlist.h"
void initSLlist(SL*psl)
{
	psl->arr = NULL;
	psl->size = 0;
	psl->capacity = 0;
}
void SLlistcapacity(SL* psl)
{
	if (psl->size == psl->capacity)
	{
		int newcapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLtype* tmp = (SLtype*)realloc(psl->arr, newcapacity * 2 * sizeof(SLtype));
		if (tmp == NULL)
		{
			printf("realloc failed\n");
			exit(-1);
		}
		else
		{
			psl->arr = tmp;
			psl->capacity = newcapacity;
		}
	}
}
void SLlistpushback(SL* psl, SLtype x)
{
	SLlistcapacity(psl);
	psl->arr[psl->size] = x;
	psl->size++;
}
void SLlistprint(SL* psl)
{
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->arr[i]);
	}
	printf("\n");
}
void SLlistpushfront(SL* psl, SLtype x)
{
	SLlistcapacity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->arr[end+1] = psl->arr[end];
		end--;
	}
	psl->size++;
	psl->arr[0] = x;
}
void SLlistpopback(SL* psl)
{
	assert(psl->size > 0);
	psl->size--;
}
void SLlistpopfront(SL* psl)
{
	int start = 0;
	assert(psl->size > 0);
	while (start+1 < psl->size)
	{
		psl->arr[start] = psl->arr[start + 1];
		start++;
	}
	psl->size--;
}
void SLlistinsert(SL* psl, SLtype x,int position)
{
	SLlistcapacity(psl);
	assert(position < psl->size);
	int start = psl->size-1;
	while (start>=position)
	{
		psl->arr[start + 1] = psl->arr[start];
		start--;
	}
	psl->arr[position] = x;
	psl->size++;
}
void SLlisterase(SL* psl, int position)
{
	assert(position < psl->size);
	int start = position+1;
	while (start < psl->size)
	{
		psl->arr[start-1] = psl->arr[start];
		start++;
	}
	psl->size--;
}